﻿#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   THis is the interface to VM Ware virtualisation
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

#Imports
Import-Module MDE-Common

Function RemoveVirtualDisk() {
Param(
[string]$MachineName
) 

    $VirtualDisk = GetVirtualDiskPath($MachineName)

    if(Test-Path $VirtualDisk) {
        Write-Host "Delete virtual disk $VirtualDisk"
        Dismount-VHD –Path $VirtualDisk -ErrorAction SilentlyContinue
        Remove-Item -Path $VirtualDisk -Force 
        }

    if(Test-Path $VirtualDisk) {
        throw "Failed to remove previous disk image"
        }
}

Function CreateVirtualDisk() {
Param(
[string]$MachineName,
[string]$TemplateName
) 

    $VirtualDisk = GetVirtualDiskPath($MachineName)
    Copy-Item -Path "$VMTemplateFolder\$TemplateName.vhdx" -Destination $VirtualDisk
}

Function MountVirtualDisk() {
Param(
[string]$MachineName,
[string]$DriveLetter
) 
    $VirtualDisk = GetVirtualDiskPath($MachineName)

    Write-Host "Select volume"
    $volume = $VirtualDisk | Get-Disk | Get-Partition | Get-Volume | Sort-Object DriveLetter -Descending | Select-Object -First 1 

    Write-Host "Acquire drive letter"
    $drive = $volume.DriveLetter

    if ($drive -eq $null) {
        throw "Failed to acquire drive letter for $VirtualDisk"
        }

    Write-Host "Create PowerShell Drive Z:"
    New-PSDrive -Name $DriveLetter -PSProvider FileSystem -Root ($drive + ":") | Out-Null
}

Function DismountVirtualDisk() {
Param(
[string]$MachineName
) 

    $VirtualDisk = GetVirtualDiskPath($MachineName)
    Dismount-VHD -Path $VirtualDisk 
}

Function GetVirtualDiskPath() {
Param(
[string]$MachineName
) 

    return "$VirtualDiskFolder\$MachineName.vhdx"
}

Function CreateVirtualMachine() {
Param(
[string]$MachineName, 
[int]$MemoryGB
) 
    $VirtualDisk = GetVirtualDiskPath($MachineName)

    New-VM -Name $MachineName -VirtualDisk $VirtualDisk -MemoryStartupBytes ($MemoryGB*1024*1024*1024)  -SwitchName $VirtualNetwork -Path $MachineFolder -BootDevice IDE | Out-Null
}

Function StartVirtualMachine() {
Param(
[string]$MachineName
) 
    Start-VM -Name $MachineName 
}

Function RemoveVirtualMachine() {
Param(
[string]$MachineName, 
[switch]$Confirm=$true
) 

    $vm = Get-VM $MachineName -ErrorAction SilentlyContinue

    if ($vm -ne $null) {
        Stop-VM $vm -Force -TurnOff -WarningAction SilentlyContinue
        Remove-VM -Name $MachineName -Force -Confirm:$Confirm
        }
}

Export-ModuleMember RemoveVirtualDisk
Export-ModuleMember CreateVirtualDisk
Export-ModuleMember MountVirtualDisk
Export-ModuleMember DismountVirtualDisk
Export-ModuleMember CreateVirtualMachine
Export-ModuleMember StartVirtualMachine
Export-ModuleMember RemoveVirtualMachine
Export-ModuleMember GetVirtualDiskPath